package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @Transactional
    public int save(DishDTO dishDTO) {
        // dish新增
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        int row = dishMapper.insert(dish);
        Long dishId = dish.getId();
        // 口味新增
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors = flavors.stream().peek(item -> {
            item.setDishId(dishId);
        }).collect(Collectors.toList());
        if (flavors != null && flavors.size() != 0) {
            dishFlavorMapper.insertBatch(flavors);
        }
        return row;
    }

    /**
     * 菜品分页
     *
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> list = dishMapper.pageQuery(dishPageQueryDTO);
        long total = list.getTotal();
        List<DishVO> records = list.getResult();
        return new PageResult(total, records);
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     * @return
     */
    @Transactional
    public int deleteBatch(List<Long> ids) {
        // 判断菜品是否启售，启售则不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            Integer status = dish.getStatus();
            if (status == 1) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        // 若菜品和套餐关联,则不能删除
        List<SetmealDish> list = setmealDishMapper.getByDishes(ids);
        if (list != null && list.size() != 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        // 删除菜品
        int row = dishMapper.deleteBatch(ids);
        // 删除菜品的口味
        dishFlavorMapper.deleteBatchByDishes(ids);
        return row;
    }

    /**
     * 根据id查询菜品
     *
     * @return
     */
    public DishVO getVOById(Long id) {
        Dish dish = dishMapper.getById(id);
        // 查询口味
        List<DishFlavor> flavors = dishFlavorMapper.getByDish(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @return
     */
    @Transactional
    public int update(DishDTO dishDTO) {
        // 修改菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        int row = dishMapper.update(dish);
        // 修改口味(先删除再添加)
        // 删除
        ArrayList<Long> list = new ArrayList<>();
        list.add(dish.getId());
        dishFlavorMapper.deleteBatchByDishes(list);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() != 0) {
            // 添加
            flavors = flavors.stream().peek(item -> {
                item.setDishId(dish.getId());
            }).collect(Collectors.toList());
            dishFlavorMapper.insertBatch(flavors);
        }
        return row;
    }

    /**
     * 菜品启售、停售
     *
     * @param id
     * @param status
     * @return
     */
    public int startOrStop(Long id, Integer status) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        return dishMapper.update(dish);
    }

    /**
     * 根据分类id查询菜品(管理端)
     * @param dish
     * @return
     */
    public List<Dish> list(Dish dish) {
        List<Dish> list = dishMapper.list(dish);
        return list;
    }

    /**
     * 根据分类id查询菜品(用户端)
     * @param dish
     * @return
     */
    public List<DishVO> listVO(Dish dish) {
        List<Dish> list = dishMapper.list(dish);
        List<DishVO> voList = list.stream().map(item -> {
            DishVO dishVO = new DishVO();
            List<DishFlavor> flavors = dishFlavorMapper.getByDish(item.getId());
            BeanUtils.copyProperties(item, dishVO);
            dishVO.setFlavors(flavors);
            return dishVO;
        }).collect(Collectors.toList());
        return voList;
    }
}
